package com.zyx.crawlerdemo.httpclient.crawler;

import java.io.IOException;
import java.net.URI;

import org.apache.http.*;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHttpResponse;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;

/**
 * @author Yaxi.Zhang
 * @since 2021/8/11 16:48
 * desc: httpclient测试案例
 */
public class HttpclientTest {
    public static void main(String[] args) throws ParseException, IOException {
        // 初始化HttpContext
        HttpContext localContext = new BasicHttpContext();
        String url = "https://searchcustomerexperience.techtarget.com/info/news";
        // 初始化httpclient
        HttpClient httpClient = HttpClients.custom().build();
        HttpGet httpGet = new HttpGet(url);
        // 执行请求获取HttpResponse
        HttpResponse httpResponse = null;
        try {
            httpResponse = httpClient.execute(httpGet, localContext);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //获取具体响应信息
        System.out.println("response:" + httpResponse);

        /*HttpClient client = new DefaultHttpClient();
        // 请求的地址URL
        String personalUrl = "https://searchcustomerexperience.techtarget.com/info/news";
        // get方法请求
        HttpGet getMethod = new HttpGet(personalUrl);
        System.out.println("Method:" + getMethod);
        // 初始化HTTP响应
        HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
                HttpStatus.SC_OK, "OK");
        // 执行响应
        response = client.execute(getMethod);
        System.out.println("response:" + response);*/

        /*HttpClient client = new DefaultHttpClient();
        // 第一种方式
        String personalUrl = "https://searchcustomerexperience.techtarget.com/info/news";
        // 创建URI
        URI uri = new URIBuilder(personalUrl).build();
        // get方法请求
        HttpGet getMethod = new HttpGet();
        // 设置
        getMethod.setURI(uri);
        // 初始化HTTP响应
        HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
                HttpStatus.SC_OK, "OK");
        // 执行响应
        response = client.execute(getMethod);*/

        // 响应状态
        String status = httpResponse.getStatusLine().toString();
        // 获取响应状态码
        System.out.println("status:" + status);
        int StatusCode = httpResponse.getStatusLine().getStatusCode();
        System.out.println("StatusCode:" + StatusCode);
        // 协议的版本号
        ProtocolVersion protocolVersion = httpResponse.getProtocolVersion();
        System.out.println("protocolVersion:" + protocolVersion);
        // 是否ok
        String phrase = httpResponse.getStatusLine().getReasonPhrase();
        System.out.println("phrase:" + phrase);
        Header[] headers = httpResponse.getAllHeaders();
        System.out.println("输出头信息为：");
		for (Header header : headers) {
			System.out.println(header);
		}
        System.out.println("头信息输出结束");
        // 状态码200表示响应成功
        if (StatusCode == HttpStatus.SC_OK) {
            //获取实体内容
            HttpEntity entity = httpResponse.getEntity();
            // 注意设置编码
            String entityString = EntityUtils.toString(entity, "gbk");
            // 输出实体内容
            System.out.println(entityString);
		}
		// 消耗实体
		EntityUtils.consume(httpResponse.getEntity());
	}
}

